import type { PaginationProps } from "../types/pagination";
import type { BasicTableProps } from "../types/table";
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from "../const";
import { isBoolean } from "@/utils/is";
import { useEffect, useMemo, useState } from "react";
import { t } from "i18next";

// function itemRender(page: number, type: string, originalElement: any): React.ReactNode {
// 	if (type === "prev") return page === 0 ? null : <LeftOutlined />;
// 	else if (type === "next") return page === 1 ? null : <RightOutlined />;
// 	return originalElement;
// }

export function usePagination(refProps: BasicTableProps) {
	const [configRef, setConfigRef] = useState<PaginationProps>({});
	const [show, setShow] = useState(true);

	useEffect(() => {
		if (!isBoolean(refProps.pagination) && refProps.pagination) {
			setConfigRef({
				...configRef,
				...(refProps.pagination ?? {})
			});
		}
	}, [refProps.pagination]);

	const getPaginationInfo = useMemo((): PaginationProps | boolean => {
		const { pagination } = refProps;
		if (!show || (isBoolean(pagination) && !pagination)) return false;
		return {
			current: 1,
			size: "small",
			defaultPageSize: PAGE_SIZE,
			showTotal: (total: number) => `${t("kevin_ui.total")}${total}条`,
			showSizeChanger: true,
			pageSizeOptions: PAGE_SIZE_OPTIONS,
			// itemRender,
			showQuickJumper: true,
			...(isBoolean(pagination) ? {} : pagination),
			...configRef
		};
	}, [configRef, refProps.pagination]);

	function setPagination(info: Partial<PaginationProps>) {
		setConfigRef({
			...(!isBoolean(getPaginationInfo) ? getPaginationInfo : {}),
			...info
		});
	}

	function getPagination() {
		return getPaginationInfo;
	}

	function getShowPagination() {
		return show;
	}

	async function setShowPagination(flag: boolean) {
		setShow(flag);
	}

	return { getPagination, getPaginationInfo, setShowPagination, getShowPagination, setPagination };
}
